package com.example.yan_music.presenter;

import android.util.Log;

import com.example.yan_music.app.XApplication;
import com.example.yan_music.base.presenter.BasePresenter;
import com.example.yan_music.bean.wangyi.Banner;
import com.example.yan_music.bean.wangyi.NewSongUrl;
import com.example.yan_music.bean.wangyi.RecommendSongList;
import com.example.yan_music.contract.IMainContract;
import com.example.yan_music.event.PlayDurationEvent;
import com.example.yan_music.utils.SharePreferenceUtil;
import com.example.yan_music.utils.XLog;
import com.example.yan_music.utils.XToast;
import com.google.gson.internal.$Gson$Preconditions;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class IMainPresenterImp  extends BasePresenter<IMainContract.View> implements IMainContract.Presenter {

    private static final String TAG = "IMainPresenterImp";

    @Override
    public void getBanner() {
        mModel.getBanner()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Banner>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "onSubscribe: ");
                    }

                    @Override
                    public void onNext(Banner banner) {
                        if(banner.getCode() == 200){
                            Log.d(TAG, "onNext: " + banner.getBanners().get(0).getPic());
                            mView.setmBanner(banner.getBanners());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete: Banner请求完成");
                    }
                });
    }

    @Override
    public void getRecommendSongLists() {
        mModel.getRecommendSongList(6)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RecommendSongList>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(RecommendSongList recommendSongList) {
                        if(recommendSongList.getCode() == 200){
                            mView.setRecommendSongLists(recommendSongList.getResult());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "onError: " + "失败");
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete: " + "推荐歌单请求完成");
                    }
                });
    }

    @Override
    public void getPlayUrl(int songId, String songName) {
        String cookie = (String) SharePreferenceUtil.get(XApplication.getContext(), "cookie", "无数据");
        Log.d(TAG, "getPlayUrl: " + songId);
        Log.d(TAG, "getSongPlayUrl: " + cookie);
        mModel.getSongPlayUrl(songId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NewSongUrl>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NewSongUrl newSongUrl) {
                        Log.d(TAG, "onNext: " + newSongUrl.getCode());
                        if(newSongUrl.getCode() == 200){
                            // 播放地址
                            String playUrl = newSongUrl.getData().get(0).getUrl();
                            // 播放时长
                            int duration;
                            // 付费歌曲只有试听片段，这里需要拿到试听时长，然后设置给SeekBar最大值
                            if(newSongUrl.getData().get(0).getFreeTrialInfo() != null) {
                                XToast.showInfo("付费歌曲只能试听哦");
                                String realDurationStr = newSongUrl.getData().get(0).getFreeTrialInfo().toString().trim();
                                String startStr = realDurationStr.substring(7, realDurationStr.indexOf(","));
                                String endStr = realDurationStr.substring(realDurationStr.indexOf(",") + 6, realDurationStr.indexOf("}"));
                                float startTime = Float.parseFloat(startStr);
                                float endTime = Float.parseFloat(endStr);
                                // 试听歌曲的时长，单位ms
                               duration = (int) (endTime - startTime) * 1000;
                                // 设置SeekBar的max
                                EventBus.getDefault().post(new PlayDurationEvent(duration, true));
                                Log.d(TAG, "onNext: 试听时长为：" + duration);
                            }
                            XLog.d("onNext: " + "该歌曲播放地址为：" + playUrl);
                            mView.playSong(playUrl);
                        }else {
                            // 提手用户播放失败
                            XToast.showError("骚瑞，好像没找到播放地址");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
